package com.example.student_management.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.student_management.entity.Student;
import com.example.student_management.mapper.StudentMapper;
import com.example.student_management.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

// 实现类
@Service
public class StudentServiceImpl implements StudentService {

    // 注入 Mapper 接口（Spring 自动装配）
    @Autowired
    private StudentMapper studentMapper;

    
    @Override
    public Page<Student> getAllStudentsByPage(int pageNum, int pageSize) {
        // 在查询之前调用 PageHelper.startPage() 方法
        PageHelper.startPage(pageNum, pageSize);
        // 后续的查询方法会被自动拦截，并生成分页的 SQL
        // 返回的 List 实际上是一个 Page 对象
        return (Page<Student>) studentMapper.selectAll();
    }

    @Override
    public Page<Student> getStudentsByKeywordAndPage(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return (Page<Student>)studentMapper.selectByKeyword(keyword);
    }

    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public int addStudent(Student student) {
       return studentMapper.insert(student);
    }

    @Override
    public int updateStudent(Student student) {
       return studentMapper.update(student);
    }

    @Override
    public int deleteStudent(Integer id) {
        return studentMapper.delete(id);
    }


}
